package com.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.sfzy.entity.Course;
import com.sfzy.entity.Search;
import com.sfzy.service.SearchService;
import com.sfzy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("search")
public class SearchController {
    @Autowired
    private SearchService searchService;

    //增
    @PostMapping
    public R add(@RequestBody Search search) {
        int i = searchService.addSearch(search);
        if (i!=0){
            return R.ok("操作成功");
        }
        return R.fail("操作失败");
    }

    //改
    @PutMapping
    public R update(@RequestBody Search search) {
        int i = searchService.updateSearch(search);
        if (i!=0){
            return R.ok("操作成功");
        }
        return R.fail("操作失败");
    }
    //删
    @DeleteMapping("{id}")
    public R delete(@PathVariable("id") int id){
        int i = searchService.deleteSearch(id);
        if (i!=0){
            return R.ok("操作成功");
        }
        return R.fail("操作失败");
    }

    //查id
    @GetMapping("{id}")
    public R get(@PathVariable("id") int id){
        Search searchs = searchService.getById(id);
        return R.ok(searchs);
    }
    //分页查
    @GetMapping("page")
    public R getByPage(Search search,
                       @RequestParam(required = false,defaultValue = "1") int pageNum,
                       @RequestParam(required = false,defaultValue = "5") int pageSize){
        PageInfo<Search> searchPage = searchService.getByPage(search, pageNum, pageSize);
        return R.ok(searchPage);
    }

}
